//
//  screenTwoView.m
//  何五路
//
//  Created by _ADY on 15/6/19.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "screenTwoView.h"

@implementation screenTwoView
@synthesize sxListArray,AttributeId,delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        vFrame = frame;
        
        myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, frame.size.width, frame.size.height - 64)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = bgColor;
        [self addSubview:myTableView];
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
        [myTableView setTableFooterView:v];
        current = 0;
        
        [self performSelector:@selector(after) withObject:nil afterDelay:.2];
        
        
    }
    return self;
}

-(void)after
{
    set = [[NSMutableArray alloc] initWithCapacity:0];
    
    for ( NSDictionary* d in sxListArray)
    {
        NSString* FlowType = [NSString stringWithFormat:@"%@",[d objectForKey:@"SpecificationAttributeId"]];
        if ([FlowType isEqualToString:[NSString stringWithFormat:@"%@",AttributeId]])
        {
            [set addObject:d];
        }
    }
    [self allButton];
    
    

    NSMutableArray *array = [screenItem unarchiveWithKey:@"screenDic"];
    for (NSDictionary *dic1 in  array)
    {
        for (int i = 0; i < set.count; i ++)
        {
            NSString* dString = [[set objectAtIndex:i] objectForKey:@"SpecificationAttributeOptionId"];
            if ([[NSString stringWithFormat:@"%@",dString] isEqualToString:[NSString stringWithFormat:@"%@",[dic1 objectForKey:@"OptionId"]]])
            {
                
                current = i+1;
                break;
            }
        }
    }
    
    
    [myTableView reloadData];
}

-(void)allButton
{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, vFrame.size.width, .5)];
    lineView.backgroundColor = [UIColor colorWithRed:191/255.0 green:191/255.0 blue:191/255.0 alpha:1];
    [self addSubview:lineView];
    
    UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [inButton setFrame:CGRectMake(0,24, 50, 40)];
    [inButton setTitle:@"取消" forState:UIControlStateNormal];
    [inButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    [inButton addTarget:self action:@selector(qxBction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:inButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,24, vFrame.size.width-100, 40)];
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = 1;
    if (set.count >0)
        titleLabel.text = [[set objectAtIndex:0] objectForKey:@"SpecificationAttributeName"];
    [self addSubview:titleLabel];
    
    UIButton *yesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [yesButton setFrame:CGRectMake(vFrame.size.width-50,24, 50, 40)];
    [yesButton setTitle:@"确定" forState:UIControlStateNormal];
    [yesButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    yesButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    [yesButton addTarget:self action:@selector(yesBction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:yesButton];
    
    
}

-(void)qxBction
{
    [self.delegate closeTwoView];
}

-(void)yesBction
{
    if (current != 0)
    {
        NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:10];
        NSMutableArray *array = [screenItem unarchiveWithKey:@"screenDic"];
        for (NSDictionary *dic1 in  array)
        {
            if ([[NSString stringWithFormat:@"%@",[[set objectAtIndex:current-1] objectForKey:@"SpecificationAttributeId"]] isEqualToString:[NSString stringWithFormat:@"%@",[dic1 objectForKey:@"AttributeId"]]])
            {
                NSMutableDictionary *dic  = [NSMutableDictionary dictionaryWithCapacity:1];
                [dic setObject:[NSString stringWithFormat:@"%@",[[set objectAtIndex:current-1] objectForKey:@"SpecificationAttributeOptionId"]] forKey:@"OptionId"];
                
                [dic setObject:[NSString stringWithFormat:@"%@",[[set objectAtIndex:current-1] objectForKey:@"SpecificationAttributeOptionName"]] forKey:@"OptionName"];
                
                [dic setObject:[NSString stringWithFormat:@"%@",[[set objectAtIndex:current-1] objectForKey:@"SpecificationAttributeId"]] forKey:@"AttributeId"];
                [newArray addObject:dic];
            }
            else
            {
                [newArray addObject:dic1];
            }
        }
        [newArray archiveWithKey:@"screenDic"];
    }

    [self.delegate closeTwoView];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return set.count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"screenTwoCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
        cell.tintColor = [UIColor colorWithRed:243/255.0 green:125/255.0 blue:125/255.0 alpha:1];//勾选颜色
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"全部";
    }
    else
        cell.textLabel.text = [[set objectAtIndex:indexPath.row-1] objectForKey:@"SpecificationAttributeOptionName"];
    cell.textLabel.textAlignment = 0;
    cell.textLabel.textColor = [UIColor blackColor];


    if(current== indexPath.row)
    {
        
        cell.accessoryType =  UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType =  UITableViewCellAccessoryNone;
    }
    // Configure the cell...
    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    current = indexPath.row;
    [myTableView reloadData];

}

@end
